package com.fc.redis.controller;

import com.fc.redis.entity.TbUser;
import com.fc.redis.service.ConcurrentCacheTestService;
import com.fc.redis.service.EhCacheTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * @program: fc-redis->CacheTestController
 * @description: 本地缓存测试
 * @author: G_Y
 * @create: 2019-09-10 15:04
 **/
@RestController
public class CacheTestController {

    @Autowired
    private EhCacheTestService ehCacheTestService;

    /**
     * 测试ehcache
     *
     * @param id
     * @return
     */
    @GetMapping("/test/ehcache/get/{id}")
    public TbUser getUserById(@PathVariable("id") Integer id) {
        TbUser user = ehCacheTestService.getUser(id);
        System.out.println(user);
        return user;
    }

    @PostMapping("/test/ehcache/save")
    public TbUser saveUseByEhCache() {
        TbUser tbUser = new TbUser();
        tbUser.setName("saveUseByEhCache");
        tbUser.setPhone("1999");
        tbUser.setUpdateTime(new Date());
        tbUser.setCreateTime(new Date());
        tbUser.setUserName("userName_EhCache");
        TbUser user = ehCacheTestService.addOrUpdateUserById(tbUser);
        System.out.println(user);
        return user;
    }


    @Autowired
    private ConcurrentCacheTestService concurrentCacheTestService;

    /**
     * 测试springboot默认缓存concurrentCache
     *
     * @param
     * @return
     */
    @PostMapping("/test/concurrent/cache/save")
    public TbUser saveUser() {
        TbUser tbUser = new TbUser();
        tbUser.setName("oooo");
        tbUser.setPhone("199");
        tbUser.setUpdateTime(new Date());
        tbUser.setCreateTime(new Date());
        tbUser.setUserName("userName_oooo");
        TbUser user = concurrentCacheTestService.addOrUpdateUserById(tbUser);
        System.out.println(user);
        return user;
    }

    @GetMapping("/test/concurrent/cache/get/{id}")
    public TbUser getUserByIdPro(@PathVariable("id") Integer id) {
        ConcurrentMapCacheManager c;
        TbUser user = concurrentCacheTestService.getUser(id);
        System.out.println(user);
        return user;
    }


}
